package filefunc

import (
	"github.com/spf13/afero"
	"gitlab.local/DO-module/new-filemanage-module/src/core/fileoperate"
	"gitlab.local/DO-module/new-filemanage-module/src/service/file_recently"
	"gitlab.local/TerraMaster/tos-modules/database"
	//"gitlab.local/TerraMaster/tos-modules/core/acl"
	"gitlab.local/TerraMaster/tos-modules/core/acl"
	"gitlab.local/golibrary/filedownload"
	"io"
	"os"
	"sync"
	"time"
)

type FileFunc struct {
	db             database.DbGetter
	fOptGetter     fileoperate.Getter
	aclService     acl.FolderAttrService
	Recently       *file_recently.Recently
	uploadStatus   string //考虑到初始化默认值，""：没有初始化，"1":重命名,"2"：覆盖,"3"：跳过
	delTimer       map[string]*FileTimer
	delRWMutex     sync.RWMutex
	xfsUpdate      map[string]*os.File //xfs文件上传句柄
	updateMap      map[string]*os.File //文件上传句柄集合
	thumbnailMutex sync.Mutex

	remoteDownloadQueue map[string]*download //远程下载队列[用户名称][下载队列]
	remoteLimitNum      uint8                //远程下载最大任务数限制
	remoteRunNum        uint8                //正在运行任务数

	fileOperationTask    map[int64]*fileoperate.FileOperationTask //保存文件操作任务
	fileOperationRWMutex sync.RWMutex

	recentlyFunc func(string, string) //文件复制或移动后，更新最近使用记录

	fs afero.Fs
}

type FileTimer struct {
	stop chan struct{}
	t    *time.Timer
}

type download struct {
	sync.Mutex
	queue []*filedownload.Download
}

type iosHttpFile struct {
	fileName string
	r        io.ReadCloser
}

type FileData struct {
	Path     string `json:"path"`
	Size     int64  `json:"size"`
	LastTime int64  `json:"last_time"`
	Md5      string `json:"md5"`
	FileType string `json:"file_type"`
}

func New(db database.DbGetter, fOptGetter fileoperate.Getter, aclService acl.FolderAttrService) *FileFunc {
	return &FileFunc{
		db:         db,
		fOptGetter: fOptGetter,
		aclService: aclService,
		Recently:   file_recently.New(db),

		delTimer:  make(map[string]*FileTimer),
		updateMap: make(map[string]*os.File),

		remoteLimitNum:      5, //限制并行任务为5个
		remoteDownloadQueue: make(map[string]*download),
	}
}
